import { prop, Ref, arrayProp } from "@typegoose/typegoose"
import { Category } from "./category.model"
import { IsUrl, IsNotEmpty } from "class-validator"
import { ApiModelProperty } from "@nestjs/swagger"
import { Comment } from './comment.model'
import { Attribute } from "./attribute.model"
import { AttributeTyps } from "../../../../apps/admin/src/categories/AttributeTyps.enum"
import { StringOrObject } from "../../../../apps/admin/src/categories/StringOrObject.enum"
import { Service } from "./service.model"

class image {
  @ApiModelProperty({ description: '图片标题', example: '图片标题' })
  @prop()
  title: string

  @ApiModelProperty({ description: '图片url', example: "https://s10.mogucdn.com/mlcdn/c45406/180913_25e804lk773hdk695c60cai492111_225x225.png" })
  @prop()
  @IsUrl()
  url: string

  @ApiModelProperty({ description: '图片权重', example: '1' })
  @prop()
  index?: number
}

enum priceType {
  RMB = 'RMB'
}

class prices {
  @ApiModelProperty({ description: '货币类型', example: priceType.RMB, enum: priceType })
  @prop()
  priceType: priceType

  @ApiModelProperty({ description: '现在价钱', example: 11 })
  @prop()
  nowPrice: Number

  @ApiModelProperty({ description: '热门价钱', example: 22 })
  @prop()
  hotPrice?: Number

  @ApiModelProperty({ description: '原价', example: 333 })
  @prop()
  initPrice?: Number
}

export class Commodity {
  @ApiModelProperty({ description: '商品标题', example: '秋装女2018新款早秋女装裙子韩版针织连衣裙+衬衫上衣时尚套装' })
  @prop()
  @IsNotEmpty({ message: '请输入商品标题' })
  title!: string

  @ApiModelProperty({ description: '商品图片', example: [{ title: '整体展示', url: 'https://img.alicdn.com/tfs/TB1EpFGu7L0gK0jSZFtXXXQCXXa-990-400.jpg_1080x1800Q90s50.jpg' }, { title: '整体展示', url: 'https://img.alicdn.com/tfs/TB1EpFGu7L0gK0jSZFtXXXQCXXa-990-400.jpg_1080x1800Q90s50.jpg' }], required: false, type: [image] })
  @prop()
  images?: image[]

  @ApiModelProperty({ description: '所属分类', example: 'objectId' })
  @prop({ ref: Category })
  @IsNotEmpty({ message: '请选择商品所属类型' })
  category!: Ref<Category>

  @ApiModelProperty({ description: '商品价格', example: { priceType: 0, nowPrice: 11, hotPrice: 8, initPrice: 20 } })
  @prop()
  prices!: prices

  @ApiModelProperty({ description: '商品描述', example: `html片段`, required: false })
  @prop()
  description?: String

  @ApiModelProperty({
    description: '属性值设定 (注意这里的 message 已经不是数据集了, 而是要展示的数据值 )', example: [{
      name: '尺码',
      type: AttributeTyps.selects,
      messageType: StringOrObject.array,
      message: ['S', 'M', 'L', 'XL'],
      value: ['S', 'M']
    },
    {
      name: '衣长',
      type: AttributeTyps.select,
      messageType: StringOrObject.number,
      message: 156,
      value: 156
    }, 
    {
      name: '号型',
      type: AttributeTyps.input,
      messageType: StringOrObject.string,
      message: '175/92A',
      value: '我是实际值'
    }],
    required: false,
    type: [Attribute]
  })
  @prop()
  attributes: Attribute[]

  @ApiModelProperty({
    description: '商品服务', example: [
      'Service._id',
      'Service._id'
    ],
    type: [String],
    required: false
  })
  @arrayProp({ itemsRef: Service  })
  services?: Ref<Service> []

  @ApiModelProperty({
    description: '商品评论', example: ['ObjectId', 'ObjectId'], type: [String], required: false
  })
  @arrayProp({ itemsRef: Comment  })
  comments?: Ref<Comment> []
}